package com.yunxin.core.infra.persistence.repository.product;

import com.yunxin.core.domain.model.product.ProductCustomConfig;
import com.yunxin.core.domain.model.product.ProductCustomConfigRepository;
import com.yunxin.core.infra.persistence.converter.ProductConverter;
import com.yunxin.core.infra.persistence.entity.product.ProductCustomConfigDO;
import com.yunxin.core.infra.persistence.mapper.product.ProductCustomConfigExtendMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

/**
 * @author zero°
 * @since 2023-07-13
 */
@Repository
@RequiredArgsConstructor
public class ProductCustomConfigRepositoryImpl implements ProductCustomConfigRepository {

    private final ProductCustomConfigExtendMapper productCustomConfigExtendMapper;

    private final ProductConverter productConverter;

    @Override
    public boolean saveProductCustomConfig(ProductCustomConfig productCustomConfig) {
        ProductCustomConfigDO productCustomConfigDO = productConverter.formProductCustomConfig(productCustomConfig);
        int count = productCustomConfigExtendMapper.insertSelective(productCustomConfigDO);
        return count > 0;
    }

    @Override
    public boolean updateProductCustomConfig(ProductCustomConfig productCustomConfig) {
        ProductCustomConfigDO productCustomConfigDO = productConverter.formProductCustomConfig(productCustomConfig);
        int count = productCustomConfigExtendMapper.updateByPrimaryKeySelective(productCustomConfigDO);
        return count > 0;
    }

    @Override
    public ProductCustomConfig getProductCustomConfigByProductId(Long productId) {
        ProductCustomConfigDO productCustomConfigDO = productCustomConfigExtendMapper.selectByProductId(productId);
        return productConverter.toProductCustomConfig(productCustomConfigDO);
    }
}
